﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CdcSoftware.Pivotal.Applications.Core.Client;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using System.Windows.Forms;
using System.Security.Cryptography;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Data;
using CdcSoftware.Pivotal.Engine.Types.Database;
using CdcSoftware.Pivotal.Engine.Types.MailMerges;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Applications.Core.Common;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_Sale_Serial_Number_Entry : FormClientTask
    {
        #region virtual Events

        public virtual void ProductBarcode_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                string lsProductBarcode = ((PivotalTextBox)sender).Text.Trim();
                if (!string.IsNullOrEmpty(lsProductBarcode))
                {
                    DataTable dtProduct = Globals.GetDataTable("RE_Product for Barcode? Alt Code1? Alt Code2? Alt Code3?",
                        new object[] { lsProductBarcode, lsProductBarcode, lsProductBarcode, lsProductBarcode },
                       new string[] { "Product_Id", "RE_Article_Number", "RE_SKU_Description" });
                    if (dtProduct != null && dtProduct.Rows.Count > 0)
                    {
                        ((PivotalTextBox)this.FormControl.GetControlByName("txtProductDescription")).Text = TypeConvert.ToString(dtProduct.Rows[0]["RE_SKU_Description"]);
                        ((PivotalTextBox)this.FormControl.GetControlByName("txtProduct")).Text = TypeConvert.ToString(dtProduct.Rows[0]["RE_Article_Number"]);
                    }
                    else
                    {
                        PivotalMessageBox.Show("No product found for Article Number: " + lsProductBarcode, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ((PivotalTextBox)this.FormControl.GetControlByName("txtProductBarcode")).Focus();
                        e.Cancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }

        public virtual void SerialNumber_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                string lsSerialNumber = ((PivotalTextBox)sender).Text.Trim();
                string lsProductBarcode = ((PivotalTextBox)this.FormControl.GetControlByName("txtProductBarcode")).Text.Trim();

                if (string.IsNullOrEmpty(lsSerialNumber) && !string.IsNullOrEmpty(lsProductBarcode))
                {
                    PivotalMessageBox.Show("The serial number can not be empty.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    e.Cancel = true;
                    return;
                }
                else if (string.IsNullOrEmpty(lsSerialNumber) && string.IsNullOrEmpty(lsProductBarcode))
                {
                    return; // --> do nothing.
                }
                // Get Product ID and Serial Number in RE_Product table
                DataTable dtProduct = Globals.GetDataTable("RE_Product for Barcode? Alt Code1? Alt Code2? Alt Code3?",
                        new object[] { lsProductBarcode, lsProductBarcode, lsProductBarcode, lsProductBarcode }, new string[] { "Product_Id", "RE_Article_Number" });

                // Check Serial Number in Product Serial Number
                DataTable serialNumberId = Globals.GetDataTable("RE_ProductSerialNo based on Serial_Number?Product?",
                    new object[] { lsSerialNumber, dtProduct.Rows[0]["Product_Id"] }, new string[] { "RE_Product_Serial_No_Id" });

                if (serialNumberId == null || serialNumberId.Rows.Count <= 0)
                {
                    if (PivotalMessageBox.Show("The serial number '" + ((PivotalTextBox)sender).Text.Trim() + "' is not available. Click Yes to continue or No to cancel.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                    {
                        e.Cancel = true;
                        return;
                    }
                }

                // check serial number existed in sale serial number ?
                foreach (DataRow dr in mdtSaleSerial.Rows)
                {
                    if (!string.IsNullOrEmpty(lsSerialNumber) && dr["Serial_Number"].ToString().Equals(lsSerialNumber))
                    {
                        ((PivotalNumber)this.FormControl.GetControlByName("numQuantity")).Value = -1;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
                e.Cancel = true;
            }
        }

        public virtual void TransactionId_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string lsSerialNumber = ((PivotalTextBox)this.FormControl.GetControlByName("txtSerialNumber")).Text.Trim();
            string lsProductBarcode = ((PivotalTextBox)this.FormControl.GetControlByName("txtProductBarcode")).Text.Trim();

            if (!string.IsNullOrEmpty(lsSerialNumber) && !string.IsNullOrEmpty(lsProductBarcode) && string.IsNullOrEmpty(((PivotalTextBox)sender).Text.Trim()))
            {
                PivotalMessageBox.Show("The Wincor Transaction Number can not be empty.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
                return;
            }
        }

        public virtual void Qty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string lsSerialNumber = ((PivotalTextBox)this.FormControl.GetControlByName("txtSerialNumber")).Text.Trim();
            string lsProductBarcode = ((PivotalTextBox)this.FormControl.GetControlByName("txtProductBarcode")).Text.Trim();

            if (!string.IsNullOrEmpty(lsSerialNumber) && !string.IsNullOrEmpty(lsProductBarcode) && !((PivotalNumber)sender).Value.HasValue )
            {
                PivotalMessageBox.Show("The Quantity must be 1 or -1.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
                return;
            }
        }

        public virtual void Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar) || e.KeyChar == '1' || e.KeyChar == '-' ) return;
            e.Handled = true;
        }

        public virtual void TransactionID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) return;
            e.Handled = true;
        }

        #endregion

    }
}
